//
//  ViewController.m
//  UI11_newPlist
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "ViewController.h"
#import "NewsModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableview;

@property (nonatomic, retain) NSMutableArray *dataSouce;


@end

@implementation ViewController

-(void)dealloc{
    
    [_tableview dealloc];
    [_dataSouce dealloc];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self handleJson];
}


- (void)creatTableView{
   
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self.view addSubview:self.tableview];
    [self.tableview release];
    
    
}

- (void)handleJson{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.dataSouce = [NSMutableArray array];
   
    for (NSDictionary *dic in result[@"news"]) {
        
      NewsModel *newsModel  = [[NewsModel alloc] init];
        
        [newsModel  setValuesForKeysWithDictionary:dic];
        [self.dataSouce addObject:newsModel];
        NSLog(@"%@", newsModel);

            }

    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouce.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    
    NewsModel *newModel = self.dataSouce[indexPath.row];
    
    
    cell.textLabel.text = newModel.title;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
